package com.cg.Controller;

import com.cg.client.SaleClient;
import com.cg.dto.CreateSalePlanDto;
import com.cg.dto.Result;
import com.cg.dto.SalePlanPageDto;
import com.cg.entity.SalePlan;
import com.cg.entity.SalePlanOperate;
import com.cg.entity.User;
import com.cg.utils.TokenUtil;
import com.cg.vo.QuerySalePlanVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SalePlanController
 * @Description TODO
 * @Author CG
 * @Date 2023/12/24 11:03
 **/
@RestController
@RequestMapping("/salePlan")
public class SalePlanController {

    @Resource
    private SaleClient saleClient;

    /**
     * 销售计划管理列表
     */
    @PostMapping("/list")
    public Result<Map<String,Object>> list(SalePlanPageDto salePlanPageDto){
        return saleClient.list(salePlanPageDto);
    }

    /**
     * 创建销售计划
     */
    @PostMapping("/createSalePlan")
    public Result createSalePlan(HttpServletRequest request, CreateSalePlanDto createSalePlanDto){
        String token = request.getHeader("token");
        Integer userId = TokenUtil.getUserIdByToken(token);
        SalePlan salePlan = createSalePlanDto.getSalePlan();
        salePlan.setCreateUser(userId);
        SalePlanOperate salePlanOperate = new SalePlanOperate();
        salePlanOperate.setCreateUser(userId);
        createSalePlanDto.setSalePlanOperate(salePlanOperate);
        return saleClient.createSalePlan(createSalePlanDto);
    }

    /**
     * 查询销售计划详细信息
     */
    @GetMapping("/querySalePlan")
    public Result<QuerySalePlanVo> querySalePlan(Integer salePlanId){
        return saleClient.querySalePlan(salePlanId);
    }


    /**
     * 加载所有的待处理人列表
     */
    @GetMapping("/pendingList")
    public Result<List<User>> pendingList(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = TokenUtil.getUserIdByToken(token);
        Result<List<User>> listResult = saleClient.pendingList(userId);
        return listResult;
    }


    /**
     * 提交销售计划
     */
    @PostMapping("/submitSalePlan")
    public Result submitSalePlan(HttpServletRequest request,CreateSalePlanDto createSalePlanDto){
        String token = request.getHeader("token");
        //获取登录的用户id
        Integer userId = TokenUtil.getUserIdByToken(token);
        createSalePlanDto.getSalePlan().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setSalePlanId(createSalePlanDto.getSalePlan().getId());
        createSalePlanDto.getSalePlanOperate().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setOperateType(2);
        return saleClient.submitSalePlan(createSalePlanDto);
    }


    /**
     * 通过销售计划
     */
    @PostMapping("/passSalePlan")
    public Result passSalePlan(HttpServletRequest request,CreateSalePlanDto createSalePlanDto){
        String token = request.getHeader("token");
        //获取登录的用户id
        Integer userId = TokenUtil.getUserIdByToken(token);
        createSalePlanDto.getSalePlan().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setSalePlanId(createSalePlanDto.getSalePlan().getId());
        createSalePlanDto.getSalePlanOperate().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setOperateType(4);
        return saleClient.passSalePlan(createSalePlanDto);
    }

    /**
     * 打回销售计划
     */
    @PostMapping("/repulshSalePlan")
    public Result repulshSalePlan(HttpServletRequest request,CreateSalePlanDto createSalePlanDto){
        String token = request.getHeader("token");
        //获取登录的用户id
        Integer userId = TokenUtil.getUserIdByToken(token);
        createSalePlanDto.getSalePlan().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setSalePlanId(createSalePlanDto.getSalePlan().getId());
        createSalePlanDto.getSalePlanOperate().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setOperateType(3);
        return saleClient.passSalePlan(createSalePlanDto);
    }


    /**
     * 作废销售计划
     */
    @PostMapping("/obsoleteSalePlan")
    public Result obsoleteSalePlan(HttpServletRequest request,CreateSalePlanDto createSalePlanDto){
        String token = request.getHeader("token");
        //获取登录的用户id
        Integer userId = TokenUtil.getUserIdByToken(token);
        createSalePlanDto.getSalePlan().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setSalePlanId(createSalePlanDto.getSalePlan().getId());
        createSalePlanDto.getSalePlanOperate().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setOperateType(5);
        return saleClient.passSalePlan(createSalePlanDto);
    }


    /**
     * 删除销售计划
     */
    @PostMapping("/removeSalePlan")
    public Result removeSalePlan(HttpServletRequest request,CreateSalePlanDto createSalePlanDto){
        String token = request.getHeader("token");
        //获取登录的用户id
        Integer userId = TokenUtil.getUserIdByToken(token);
        createSalePlanDto.getSalePlan().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setSalePlanId(createSalePlanDto.getSalePlan().getId());
        createSalePlanDto.getSalePlanOperate().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setOperateType(8);
        return saleClient.removeSalePlan(createSalePlanDto);
    }


    /**
     * 修改销售计划
     */
    @PostMapping("/updateSalePlan")
    public Result updateSalePlan(HttpServletRequest request,CreateSalePlanDto createSalePlanDto){
        String token = request.getHeader("token");
        //获取登录的用户id
        Integer userId = TokenUtil.getUserIdByToken(token);
        createSalePlanDto.getSalePlan().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setSalePlanId(createSalePlanDto.getSalePlan().getId());
        createSalePlanDto.getSalePlanOperate().setCreateUser(userId);
        createSalePlanDto.getSalePlanOperate().setOperateType(7);
        return saleClient.removeSalePlan(createSalePlanDto);
    }
}
